package com.yfree.libs.letterscroll.main;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.yfree.R;
import com.yfree.libs.letterscroll.adapter.SortAdapter;
import com.yfree.libs.letterscroll.entity.LetterViewItemEntity;
import com.yfree.libs.letterscroll.utils.CharacterParser;
import com.yfree.libs.letterscroll.utils.PinyinComparator;
import com.yfree.libs.letterscroll.view.ClearEditText;
import com.yfree.libs.letterscroll.view.SideBar;
import com.yfree.libs.letterscroll.view.SideBar.OnTouchingLetterChangedListener;

import java.util.ArrayList;
import java.util.Collections;

public class LetterView<T extends LetterViewItemEntity> {
    private ClearEditText mClearEditText;
    private ListView sortListView;
    private SideBar sideBar;
    private TextView dialog;
    private CharacterParser characterParser;
    private PinyinComparator pinyinComparator;
    private ArrayList<T> SourceDateList;
    private SortAdapter<T> adapter;
    private Context context;
    public ArrayList<T> filterDateList;

    public LetterView(Context context, View view) {
        this.context = context;
        mClearEditText = (ClearEditText) view.findViewById(R.id.filter_edit);
        sortListView = (ListView) view.findViewById(R.id.country_lvcountry);
        sideBar = (SideBar) view.findViewById(R.id.sidrbar);
        dialog = (TextView) view.findViewById(R.id.dialog);
        initViews();
    }

    public void init(ArrayList<T> SourceDateList) {
        this.SourceDateList = filledData(SourceDateList);
        // 根据a-z进行排序源数据
        Collections.sort(this.SourceDateList, pinyinComparator);
        adapter = new SortAdapter<T>(context, this.SourceDateList);
        sortListView.setAdapter(adapter);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        sortListView.setOnItemClickListener(onItemClickListener);
    }

    private void initViews() {
        // 实例化汉字转拼音类
        characterParser = CharacterParser.getInstance();
        pinyinComparator = new PinyinComparator();
        sideBar.setTextView(dialog);

        // 设置右侧触摸监听
        sideBar.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {

            @Override
            public void onTouchingLetterChanged(String s) {
                // 该字母首次出现的位置
                int position = adapter.getPositionForSection(s.charAt(0));
                if (position != -1) {
                    sortListView.setSelection(position);
                }

            }
        });

        // 点击每一个品牌跳转
        sortListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // 这里要利用adapter.getItem(position)来获取当前position所对应的对象

            }
        });

        // 根据输入框输入值的改变来过滤搜索
        mClearEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                // 当输入框里面的值为空，更新为原来的列表，否则为过滤数据列表
                filterData(s.toString());
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    private ArrayList<T> filledData(ArrayList<T> date) {
        for (int i = 0; i < date.size(); i++) {
            LetterViewItemEntity sortModel = date.get(i);
            // 汉字转换成拼音
            String pinyin = characterParser.getSelling(sortModel
                    .getItemShowText());
            String sortString = pinyin.substring(0, 1).toUpperCase();
            // 正则表达式，判断首字母是否是英文字母
            if (sortString.matches("[A-Z]")) {
                sortModel.setSortLetters(sortString.toUpperCase());
            } else {
                sortModel.setSortLetters("#");
            }
        }
        return date;
    }

    /**
     * 根据输入框中的值来过滤数据并更新ListView
     *
     * @param filterStr
     */
    private void filterData(String filterStr) {
        filterDateList = new ArrayList<T>();

        if (TextUtils.isEmpty(filterStr)) {
            filterDateList = SourceDateList;
        } else {
            filterDateList.clear();
            for (LetterViewItemEntity sortModel : SourceDateList) {
                String name = sortModel.getItemShowText();
                // ！=-1含有该汉字就添加，==0必须为开始的时候
                // startWith必须以输入的值为开始
                // contains含有就可以
                if (name.indexOf(filterStr.toString()) == 0
                        || characterParser.getSelling(name).startsWith(
                        filterStr.toString())) {
                    filterDateList.add((T) sortModel);
                }
            }
        }

        // 根据a-z进行排序
        Collections.sort(filterDateList, pinyinComparator);
        adapter.updateListView(filterDateList);
    }
}
